//
//  AddFrinedViewController.m
//  WhatsChat
//
//  Created by lanou3g on 16/3/24.
//  Copyright © 2016年 tanchen. All rights reserved.
//

#import "AddFrinedViewController.h"
#import "UserManger.h"
#import "ContactsTableViewCell.h"
@interface AddFrinedViewController ()
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic, strong) NSArray *userArray;

@end

@implementation AddFrinedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏tabBarController
    self.tabBarController.tabBar.hidden = YES;
    // 设置导航栏所有按钮标题和按钮图像的颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    //导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.1949 green:0.6774 blue:0.2191 alpha:1.0];
    //设置透明度
    self.navigationController.navigationBar.translucent = YES;
    //导航栏标题
    self.navigationItem.title = @"查找好友";
    //设置字体
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //代理
    [_searchBar setDelegate:self];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    //查找已经注册过的人
    [self searchUser:@""];
    self.userArray = [NSArray array];
    //注册自定义cell
    [self.tableView registerClass:[ContactsTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
}

- (void)backAction{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

//查找好友
- (void)searchUser:(NSString *)name {
    [[UserManger manager] findUsersByPartname:name withBlock: ^(NSArray *objects, NSError *error) {
        if (!error) {
            if (objects) {
                self.userArray = objects;
                [_tableView reloadData];
                
            }
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.userArray count];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0f;
}
//根据seachbar里输入的内容查找好友
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    NSString *content = searchBar.text;
    [self searchUser:content];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ContactsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[ContactsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    AVUser *user = self.userArray[indexPath.row];
    cell.userNameLabel.text = user.username;
    
    return cell;

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
